﻿using System;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Moonwish.Silverlight.OsAvatar.Programs
{
    /// <summary>
    /// 应用程序基类，可继承此类以实现系统内菜单功能、外部菜单功能、xap下载菜单安装等功能。
    /// by zhlin 2012-02-05。
    /// </summary>
    public abstract class ProgramWrapper
    {
        protected static Canvas _windowWrapper;
        protected static StackPanel _taskWrapper;
        private readonly static ObservableCollection<ProgramWrapper> _deskTopPrograms = new ObservableCollection<ProgramWrapper>();

        /// <summary>
        /// 设置运行环境。
        /// </summary>
        /// <param name="windowWrapper"></param>
        /// <param name="taskWrapper"></param>
        internal static void SetEnvironment(Canvas windowWrapper, StackPanel taskWrapper)
        {
            _windowWrapper = windowWrapper;
            _taskWrapper = taskWrapper;
        }

        protected ProgramWrapper()
        {
            Id = Guid.NewGuid().ToString();
            ResizeEnabled = true;
            Icon32 = new BitmapImage(new Uri("/Moonwish.Silverlight.OsAvatar;component/Icons/app64.png", UriKind.Relative));
            Children = new ProgramCollection(this);
        }

        internal static ObservableCollection<ProgramWrapper> DesktopPrograms
        {
            get { return _deskTopPrograms; }
        }

        public abstract void Show();

        internal static string GetDesktopPath(ProgramWrapper p)
        {
            var parent = p;
            var fullPath = new StringBuilder();
            string path = string.Empty;
            while (parent.Parent != null)
            {
                parent = parent.Parent;
                path = @"\" + parent.Name + path;
            }
            fullPath.Append("桌面:");
            fullPath.Append(path);
            fullPath.Append(@"\");
            fullPath.Append(p.Name);
            return fullPath.ToString();
        }

        public bool ResizeEnabled { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string Id { get; set; }

        public string Name { get; set; }

        private bool _isShowDesktop;
        /// <summary>
        /// 是否显示到桌面。
        /// </summary>
        public bool IsShowDesktop { 
            get { return _isShowDesktop; }
            set { 
                _isShowDesktop = value;

                if (!DesktopPrograms.Contains(this) && value)
                {
                    DesktopPrograms.Add(this);
                }
                if (DesktopPrograms.Contains(this) && !value)
                {
                    DesktopPrograms.Remove(this);
                }
            }
        }

        public string FullPath { get; set; }

        /// <summary>
        /// 
        /// </summary>
        internal ProgramWrapper Parent { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ProgramCollection Children { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ImageSource Icon32 { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ImageSource Icon16 { get; set; }
    }
}
